import { Component, OnInit, Input } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UserService } from 'src/app/core/service/user.service';

@Component({
  selector: 'app-edit-org-user',
  templateUrl: './edit-org-user.component.html',
  styleUrls: ['./edit-org-user.component.scss']
})
export class EditOrgUserComponent implements OnInit {
  @Input() orgName: string;
  @Input() orgUuid: string;

  editOrgUserForm: FormGroup;
  isHidden = false;
  info = {};

  nameFlag = '';

  constructor(
    private modal: NzModalRef,
    private message: NzMessageService,
    private userService: UserService,
    fb: FormBuilder
  ) {
    this.editOrgUserForm = fb.group({
      orgName: ['', Validators.required],
    });
  }


  updateOrgAdmin() {
    console.log('提交', this.editOrgUserForm.controls.orgName.value);
    // tslint:disable-next-line:forin
    for (const c in this.editOrgUserForm.controls) {
      this.editOrgUserForm.controls[c].markAsTouched();
    }

    if (this.editOrgUserForm.valid) {
      this.info = {
        orgName: this.editOrgUserForm.controls.orgName.value,
        orgUuid: this.orgUuid
      };
      this.editService(this.info, '修改');
    } else {
      this.message.error('表单校验失败', { nzDuration: 10000 });
    }
  }


  private editService(info: any, msg: string) {
    this.userService.updateOrgUser(info)
      .subscribe((OrgUserData) => {
        console.log(OrgUserData);
        if (OrgUserData.status) {
          this.message.success(msg + '成功!', {
            nzDuration: 10000
          });
          this.modal.destroy();
        } else {
          this.message.error(msg + '失败!请检查后重新填写', {
            nzDuration: 10000
          });
        }
      });
  }

  destroyModal(): void {
    this.modal.destroy({ data: 'this the result data' });
  }

  ngOnInit() {
    this.editOrgUserForm.patchValue({
      orgName: this.orgName
    });
  }

}
